package com.wanghua.mall.order.webcontroller;

import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.wanghua.mall.order.service.AlipayService;
import com.wanghua.mall.order.service.OrderService;
import com.wanghua.mall.order.vo.PayVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wanghua
 * @create 2021-09-10 19:33
 */
@RestController
public class AlipayController {

    @Resource
    private AlipayService alipayService;
    @Resource
    private OrderService orderService;


    @RequestMapping("/alipayOrder")
    public void createWebTrade(@RequestParam(value = "orderNo", required = false) String orderNo, HttpServletResponse response) throws Exception {
        PayVo orderPay = orderService.getOrderPay(orderNo);
        String tradeNo = orderPay.getTradeNo();
        String subject = orderPay.getSubject();
        String totalAmount = orderPay.getTotalAmount();
        String returnUrl = orderPay.getReturnUrl();
        AlipayTradePagePayResponse pagePayResponse = alipayService.createWebTradeForm(tradeNo, subject, totalAmount, returnUrl);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(pagePayResponse.getBody());// 直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }

    @RequestMapping(value = "webReturnUrl", method = RequestMethod.GET)
    public Object webTradeReturnUrl(HttpServletRequest request) {
        System.out.println(request.getParameterMap());
        return request.getParameterMap();
    }

}
